<template>
  <div style="height:100vh;background-image:linear-gradient(90deg,#1352F2,#82abe8);overflow:hidden;">
    <el-container style="padding:30px;">
      <el-header>
        <el-row :gutter="10">
          <el-col :span="3">

          </el-col>
          <el-col :span="1">

          </el-col>
          <el-col :span="1">

          </el-col>
          <el-col :span="19" style="text-align: left;">
            <span style="color:#fff;font-size:30px;font-weight:bold;">智能空间管理平台</span>
          </el-col>
        </el-row>
      </el-header>
      <el-main>
        <el-row :gutter="40">
          <el-col :span="16">

          </el-col>
          <el-col :span="6">
            <el-card style="margin-top:80px;" v-show="isLogin">
              <h3 style="text-align:center;">用户登录</h3>
              <el-form label-position="top">
                <el-form-item label="用户名">
                  <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                  <el-input placeholder="请输入密码" v-model="user.password" type="password" @keydown.enter="login()"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" style="width:100%;" @click="login()">登录</el-button>
                </el-form-item>
              </el-form>
              <div style="color:#c2c2c2;text-align:center;font-size:14px;padding:10px 0;position: relative">
                <span>还没有账号?</span>
                <el-button type="text" style="position: relative;top: -2px;" @click="isLogin = !isLogin">点击此处注册</el-button>
              </div>
            </el-card>
            <el-card style="margin-top:80px;" v-show="!isLogin">
              <h3 style="text-align:center;">注册</h3>
              <el-form label-position="top">
                <el-form-item label="用户名">
                  <el-input placeholder="请输入用户名" v-model="reg.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                  <el-input placeholder="请输入密码" v-model="reg.password" type="password"></el-input>
                </el-form-item>
                <el-form-item label="昵称">
                  <el-input placeholder="请输入昵称" v-model="reg.nickname"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                  <el-select placeholder="请选择您的性别" v-model="reg.gender" type="gender">
                    <el-option v-for="genders in genderOptions" :key="genders.value" :label="genders.label" :value="genders.value"/>
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" style="width:100%;" @click="regUser()">注册</el-button>
                </el-form-item>
              </el-form>
              <div style="color:#c2c2c2;text-align:center;font-size:14px;padding:10px 0;position: relative">
                <span>已有账号?</span>
                <el-button type="text" style="position: relative;top: -2px;" @click="isLogin = !isLogin">点击此处登录</el-button>
              </div>
            </el-card>
          </el-col>
          <el-col :span="2">

          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>


<script setup>
import {ref} from "vue";
import axios from "axios";
import router from "@/router";
import {ElMessage} from "element-plus";
import qs from "qs";

const user = ref({username: '', password: ''});
const reg = ref({username: '', password: '',nickname: '',gender: ''});

const isLogin = ref(true);

const genderOptions = [
  {
    value: '男',
    label: '男',
  },
  {
    value: '女',
    label: '女',
  },
]

//登录方法
const login = () => {
  let data = qs.stringify(user.value);
  console.log(data);
  //Security默认代理接口为login
  axios.post(BASE_URL+'/isms/user/login', data).then(response => {
    if (response.data.code == 1001) {
      ElMessage.success('登录成功!');
      localStorage.setItem('user', JSON.stringify(response.data.data));
      router.push('/home');
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

const regUser = () => {
  let data = qs.stringify(reg.value);
  console.log(data);
  axios.post(BASE_URL+'/isms/user/register', data).then(response => {
    if (response.data.code == 1010) {
      ElMessage.success(response.data.msg);
      setTimeout(function (){
        location.reload();
      },1000);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

</script>

<style scoped>
</style>